package com.sooth.system.menu.mapper;

import com.dream.system.core.resultsethandler.ResultSetHandler;
import com.dream.system.core.resultsethandler.TransformResultSetHandler;
import com.dream.system.provider.ActionProvider;
import com.dream.util.tree.TreeUtil;

import java.util.Collection;
import java.util.List;

public class SysMenuProvider {
    public ActionProvider selectMenuTree() {
        return new ActionProvider() {

            @Override
            public String sql() {
                return """
                        SELECT
                        	@*()
                        FROM
                        	sys_menu
                        WHERE
                        	id IN (
                        	SELECT
                        		m.id
                        	FROM
                        		sys_menu m
                        		LEFT JOIN sys_role_menu rm ON m.id = rm.menu_id
                        		LEFT JOIN sys_user_role sur ON rm.role_id = sur.role_id
                        		LEFT JOIN sys_role ro ON sur.role_id = ro.id
                        		LEFT JOIN sys_user u ON sur.user_id = u.id
                        	WHERE @non(
                        		u.id = :userId
                            	AND m.menu_name LIKE concat( '%', :menu.menuName, '%' )
                        		AND m.menu_type IN (@foreach(menu.menuType))
                        		)
                        	)
                        ORDER BY
                        	parent_id,
                        	order_num
                        """;
            }

            //            @Override
//            public DestroyAction destroyAction() {
//                return (result, mappedStatement, session) -> TreeUtil.toTree((Collection<? extends Tree>) result);
//            }
            @Override
            public ResultSetHandler resultSetHandler() {
                return new TransformResultSetHandler<Collection, List>(result -> TreeUtil.toTree(result));
            }
        };
    }
}
